package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

//TODO 为啥要加service？？
@Service
public class SettingsService {


    @DubboReference
    SettingsApi settingsApi;

    @DubboReference
    QuestionApi questionApi;

    @DubboReference
    BlackListApi blackListApi;

    public SettingsVo settings() {
        SettingsVo settingsVo = new SettingsVo();
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();
        Settings settings = settingsApi.findByUserId(userId);
        Question question = questionApi.findByUserId(userId);

        settingsVo.setId(userId);
        settingsVo.setPhone(mobile);
        String questionText = question == null ? " 默认问题" : question.getTxt();
        settingsVo.setStrangerQuestion(questionText);
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
        }

        return settingsVo;
    }

    public void saveQuestions(String content) {
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        if (question == null) {
            question = new Question();
            question.setUserId(UserHolder.getUserId());
            question.setTxt(content);
            questionApi.save(question);
        } else {
            question.setTxt(content);
            questionApi.update(question);
        }

    }

    /**
     * 通知设置
     *
     * @param map
     */
    public void notification(Map map) {
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.insert(settings);
        } else {
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }


    }

    //分页查询黑名单列表
    public PageResult blacklist(int page, int size) {
        //1、获取当前用户的id
        Long userId = UserHolder.getUserId();
        //2、调用API查询用户的黑名单分页列表  Ipage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords());
        //4、返回
        return pr;
    }

    public void deleteBlackList(Long blackUserId) {
        blackListApi.delete(UserHolder.getUserId(),blackUserId);
    }
}
